package myLink;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * Description:
 * Author:    Dane
 * CreateTime:2024/8/20-下午5:08
 * Since  :   1.0
 * Version:   1.0
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class MyHeroLink {
    public Hero header;

    public void add(Hero hero) {
        Hero temp = header;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;

        }
        temp.next = hero;


    }

    public void addByNo(Hero hero) {
        Hero temp = header;
        boolean flag = false;
        while (true) {

            if (temp.next == null)
                break;
            if (hero.no == temp.next.no) {
                flag = true;
                break;

            } else if (temp.next.no > hero.no) {

                break;
            }


            temp = temp.next;

        }
        if (flag) {
            System.out.println("no重复");
        } else {
            if(temp.next==null){
                add(hero);
                return;
            }

            hero.next = temp.next;
            temp.next = hero;
        }


    }

    public void del(int no) {
        if (header.next == null) {
            System.out.println("空链表");
            return;
        }

        boolean flag = false;
        Hero temp = header;
        while (true) {

            if (temp.next.no == no) {
                flag = true;
                break;
            }

            temp = temp.next;
        }
        if (flag) {
            temp.next = temp.next.next;
        } else {
            System.out.println("链表不存在no=" + no + "的元素");
        }


    }

    public void show() {
        if (header.next == null) {
            System.out.println("空链表");
            return;
        }
        Hero temp = header.next;


        while (true) {
            System.out.println(temp);
            if (temp.next == null) {
                break;
            }
            temp = temp.next;

        }


    }

    public Hero reverseLink(Hero head) {
//        if (head == null || head.next == null) {
//            return head;
//        }
//        Hero cur = head;
//        Hero pre = null;
//        if (cur != null) {
//            Hero next = cur.next;
//            cur.next = pre;
//            pre = cur;
//            cur = next;
//        }

        if (head == null || head.next == null) {
            return head;

        }

//        Hero cur = head;
//        Hero pre = null;
//        Hero next = null;
//        while (cur != null) {
//            next = cur.next;
//            cur.next = pre;
//            pre = cur;
//            cur = next;
//        }

        Hero cur=head;
        Hero pre=null;
        Hero next=null;
        while (cur!=null){
            next=cur.next;
            cur.next=pre;
            pre=cur;
            cur=next;
        }
        Hero reverseLink = new Hero(0, "", "", pre);

        return reverseLink;
    }


}
